package com.cheng.commonutlis.avtivity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.databinding.DataBindingUtil;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.cheng.commonlibrary.activity.CommonActivity;
import com.cheng.commonutlis.R;
import com.cheng.commonutlis.databinding.ActivityMainBinding;
import com.cheng.commonutlis.mvvm.activity.MvvmActivity;
import com.cheng.commonutlis.recyclerviewtest.utils.JsonBean;
import com.cheng.commonutlis.recyclerviewtest.utils.LevelsListDate;
import com.cheng.imagerelated.uTakePhoto.manager.UTakePhoto;

import java.util.ArrayList;

import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.OnNeverAskAgain;
import permissions.dispatcher.OnPermissionDenied;
import permissions.dispatcher.RuntimePermissions;

@RuntimePermissions
public class MainActivity extends CommonActivity {

    private ActivityMainBinding binging;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initHyPickerView();

//        binging.dataBinding

        spanTest();
    }

    @Override
    public void setContentView(int layoutResID) {
        binging = DataBindingUtil.inflate(getLayoutInflater(), layoutResID, null, false);


        super.setContentView(binging.getRoot());
    }

    private void spanTest() {

//        SpanUtils.with(new TextView(this))
//                .setTypeface()
    }

    @Override
    protected int loadLayoutResId() {
        return 0;
    }

    public void onClick(View view) {
        startActivity(new Intent(getActivity(), Main2Activity.class));
    }

    public void guide_on_Click(View view) {
        startActivity(new Intent(getActivity(), Main5Activity.class));
    }

    public void recycler_view_on_click(View view) {
        startActivity(new Intent(getActivity(), RecyclerActivity.class));
    }

    public void gomvvm(View view) {
        startActivity(new Intent(getActivity(), MvvmActivity.class));
    }


    //////////////////省市区三级联动/////////////////////////////////////////////////////////////////////////////////////
    private OptionsPickerView pvOptions;
    private LevelsListDate levelsListDate;
    private ArrayList<JsonBean> jsonBeans;
    private ArrayList<ArrayList<String>> arrayLists;
    private ArrayList<ArrayList<ArrayList<String>>> arrayLists1;
    private Handler handler1 = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            try {
                showHyPickerView();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };


    public void on_provinces(View view) {
        if (pvOptions != null) {
            pvOptions.show();
        }
    }

    private void initHyPickerView() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    levelsListDate = new LevelsListDate(MainActivity.this);
                    jsonBeans = levelsListDate.initJsonData("citys_data.json");
                    arrayLists = levelsListDate.initJsonData1("citys_data.json");
                    arrayLists1 = levelsListDate.initJsonData2("citys_data.json");
                    handler1.sendEmptyMessage(1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }

    /**
     * 初始化城市选择器
     */
    private void showHyPickerView() {
        //条件选择器
        pvOptions = new OptionsPickerBuilder(MainActivity.this, new com.bigkoo.pickerview.listener.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                try {
                    binging.provinces.setText(jsonBeans.get(options1).getCity().get(options2).getRes().get(options3).getREGION_NAME());
                } catch (Exception e) {
                    // cityId = .get(options1).getCity().get(options2).getID() + "";
                    binging.provinces.setText(jsonBeans.get(options1).getCity().get(options2).getREGION_NAME());
                }
            }
        }).build();
        pvOptions.setPicker(jsonBeans, arrayLists, arrayLists1);
    }

    public void imageRelated(View view) {
        // 图片相关操作
        startActivity(new Intent(this, TakePhotoActivity.class));
    }

    @Override
    protected void onStart() {
        super.onStart();
        MainActivityPermissionsDispatcher.applayPermissionsWithPermissionCheck(this);
    }



    @NeedsPermission({Manifest.permission.BLUETOOTH,
            Manifest.permission.CAMERA,
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.ACCESS_NETWORK_STATE,
            Manifest.permission.ACCESS_WIFI_STATE,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION})
    public void applayPermissions() {
    }

    // 用户拒绝授权回调（可选）
    @OnPermissionDenied({Manifest.permission.BLUETOOTH,
            Manifest.permission.CAMERA,
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.ACCESS_NETWORK_STATE,
            Manifest.permission.ACCESS_WIFI_STATE,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION})
    public void applayDenied() {
        new AlertDialog.Builder(this)
                .setCancelable(false)
                .setTitle("温馨提示")
                .setMessage("由于您拒绝了一些APP必须使用的权限，可能会导致APP无法正常使用！")
                .setPositiveButton("取消", (dialog, which) -> {
                    dialog.dismiss();
                    finish();
                })
                .setNegativeButton("重新请求", (dialog, which) -> {
                    dialog.dismiss();
                    MainActivityPermissionsDispatcher.applayPermissionsWithPermissionCheck(this);
                })
                .show();
    }

    // 用户勾选了“不再提醒”时调用（可选）
    @OnNeverAskAgain({Manifest.permission.BLUETOOTH,
            Manifest.permission.CAMERA,
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.ACCESS_NETWORK_STATE,
            Manifest.permission.ACCESS_WIFI_STATE,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.ACCESS_FINE_LOCATION})
    public void applayAskAgain() {
        new AlertDialog.Builder(this)
                .setCancelable(false)
                .setTitle("温馨提示")
                .setMessage("由于您禁止了一些APP必须使用的权限，可能会导致APP无法正常使用！")
                .setPositiveButton("取消", (dialog, which) -> {
                    dialog.dismiss();
                    finish();
                })
                .setNegativeButton("去设置", (dialog, which) -> {
                    dialog.dismiss();
                })
                .show();
    }

    @SuppressLint("NeedOnRequestPermissionsResult")
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        MainActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
    }
}
